home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / VideoToolbox 96.06.15 / VideoToolboxSources / IsCmdPeriod.c < prev    next >
Text File  |  1996-06-06  |  5KB  |  113 lines

  1. /*
  2. IsCmdPeriod.c
  3. You pass it an event record and it tells you whether or not the user has hit
  4. command-period (i.e. "cancel"). This method should work on all Macs, including
  5. international MacOS that remap the keyboard.
  6.  
  7. WARNING:
  8. The simple call probably works fine, but I haven't tested it.
  9. Nor have I tested the elaborate scheme. I haven't read the Script Manager chapter. The caching
  10. scheme (saving "period") might fail if you change script systems between calls
  11. to IsCmdPeriod.
  12.  
  13. Copied from comp.sys.mac.programming:
  14.  
  15. From: k044477@hobbes.kzoo.edu (Jamie R. McCarthy)
  16. Date: 13 May 93 19:41:38 GMT
  17. Organization: Kalamazoo College
  18.  
  19. >PLEASE everybody read the "International Canceling" tech note (in
  20. >"Text"), or in the official jargon: M.TE.InternationalCancel (yech).
  21. >
  22. >It explains that Command key combinations with non-letter keys may
  23. >map to Command-Shift key codes on international keyboards. And we
  24. >all know that Command-Shift gets intercepted by the operating system.
  25. >This is a pain, because it means you won't be able to cancel operations,
  26. >or have access to similar keyboard shortcuts.
  27.  
  28. Here's the function I use.  I think I took it pretty much straight from
  29. the TN, but I might have made some minor improvement, I don't recall
  30. what.  Anyway, maybe this'll save someone a few minutes' worth of
  31. typing:
  32. - -- 
  33.  Jamie McCarthy     Internet: k044477@kzoo.edu    AppleLink: j.mccarthy
  34.  
  35. HISTORY:
  36. 13 May 93 Jamie McCarthy posted it on comp.sys.mac.programming
  37. 6/23/93    dgp corrected bug of using "=" instead of "==" in an if statement.
  38.     Check for valid handle before dereferencing. Tightened up code a bit.
  39.     Not tested. 
  40. 6/30/93    dgp The elaborate method required for international systems seems
  41. excessive for routine use, so I added a shortcut. We go through the whole 
  42. rigamarole once, and note what the Cmd-period key presses were translated to.
  43. On subsequent occasions we just (quickly) check for that. 
  44. 5/22/95 dgp Apple changed the prototype from KeyTrans(...,long *state) 
  45.         to KeyTranslate(...,unsigned long *state). To make the code compatible
  46.         with both versions, I cast the argument as (void *).
  47. 11/10/95 dgp Daniel Sears, sears@netcom.com, writes "IsCmdPeriod.c should take advantage of
  48. IsCmdChar(event, '.'), which is international and was divulged in a Develop Q/A."
  49. I looked for this on the current Apple Developer CD (November '95) and the two preceding ones,
  50. and can't find it.I did find Tech Note TE 23 International Canceling, dated 1990, which 
  51. clearly predates the existence of IsCmdChar and goes into a long
  52. explanation and supplies sample code, which I suppose is the basis of what's below, though
  53. I haven't checked. I did find the prototype for IsCmdChar in Script.h though, and went ahead and made
  54. the change, as suggested by Dan.
  55. */
  56. #include "VideoToolbox.h" 
  57. //#include <Types.h>
  58. //#include <Script.h>
  59. //#include <OSUtils.h>
  60. #if !UNIVERSAL_HEADERS
  61.     extern pascal Boolean IsCmdChar(const EventRecord *event, short test)={0x2F3C,0x8206,0xFFD0,0xA8B5};
  62. #endif
  63.  
  64. Boolean IsCmdPeriod(register EventRecord *event)
  65. {
  66.     static char period=0;    // what cmd-period gets mapped to.
  67.     static Boolean firstTime=1,trapAvailable;
  68.  
  69.     if(firstTime){
  70.         // I don't know whether this test is sufficient to guarantee presence of IsCmdChar().
  71.         #define _ScriptUtil 0xA8B5
  72.         trapAvailable=TrapAvailable(_ScriptUtil);
  73.         firstTime=0;
  74.     }
  75.     if(trapAvailable)
  76.         return IsCmdChar(event,'.');    // prototype in Script.h. Documented in Mac Tech Q&A??
  77.     else{
  78.         if(period!=0) return (event->what == keyDown || event->what == autoKey)
  79.             && (event->modifiers & cmdKey)
  80.             && (event->message & charCodeMask) == period;
  81.         else{
  82.             Boolean isCmdPeriod=FALSE;
  83.             short keyCode;
  84.             long keyInfo,keyCID;
  85.             unsigned long state;
  86.             Handle hKCHR;
  87.             Ptr pKCHR;
  88.             
  89.             if ((event->modifiers & cmdKey)
  90.                 && (event->what == keyDown || event->what == autoKey)){
  91.                 pKCHR=(Ptr)GetEnvirons(smKCHRCache);    // returns NULL under System 6
  92.                 if(pKCHR == NULL){
  93.                     keyCID=GetScript(GetEnvirons(smKeyScript),smScriptKeys);
  94.                     hKCHR=GetResource('KCHR',keyCID);
  95.                     if(hKCHR!=NULL)pKCHR=*hKCHR;
  96.                 }else hKCHR=NULL;
  97.                 if(pKCHR != NULL){
  98.                     // re-translate the virtual key to a char, without the command modifier
  99.                     keyCode=(event->message & keyCodeMask) >> 8;    // the virtual key
  100.                     keyCode |= event->modifiers & 0xFF00 & ~cmdKey;
  101.                     state=0;
  102.                     keyInfo=KeyTrans(pKCHR,keyCode,(void *)&state);    // compatible with all prototypes
  103.                     if((keyInfo&charCodeMask) == '.' 
  104.                         || ((keyInfo>>16)&charCodeMask) == '.')isCmdPeriod=TRUE;
  105.                     if (hKCHR != NULL) ReleaseResource(hKCHR);
  106.                 }else if((event->message&charCodeMask) == '.')isCmdPeriod=TRUE;
  107.                 if(isCmdPeriod)period=event->message&charCodeMask;
  108.             }
  109.             return isCmdPeriod;
  110.         }
  111.     }
  112. }
  113.